package com.wx.future.wx;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.LockSupport;

/**
 * @description:
 * @author: wangxun
 * @createDate: 2022/1/14
 * @version: 1.0
 */
public class ComeTask<V> implements RunnableCome<V>{


    private Rs rs;
    private List<Thread> waitList = new ArrayList<>();

    public ComeTask(Rs rs) {
        this.rs = rs;
    }

    // 未来的返回值
    private V comeValue ;


    public V get() {
        if(comeValue == null){
            waitList.add(Thread.currentThread());
            LockSupport.park(this);

        }

        return comeValue;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        comeValue  = (V) rs.execute();

        if(waitList.size() != 0 ){
            for (Thread thread : waitList) {
                LockSupport.unpark(thread);
            }
        }

    }
}
